﻿using MediatR;
using RedDragon.Common.Domain.InventoryAllocate;
using RedDragon.Common.Infrastructure.Interface;
using T.Common.Domain.Common;

namespace RedDragon.RBAC.API.Application.Common.Inventoryscheduling
{
    public class InventoryDetailCreateHandler : IRequestHandler<InventoryDetailCreateCommon, Result>
    {
        public readonly IBaseRepository<InventoryDetail> InventoryDetailRepository;

        public InventoryDetailCreateHandler(IBaseRepository<InventoryDetail> inventoryDetailRepository)
        {
            InventoryDetailRepository = inventoryDetailRepository;
        }

        public async Task<Result> Handle(InventoryDetailCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = 0, Msg = "库存调拨详情新增失败" };
            try
            {
                var res = new InventoryDetail()
                {
                    AllocateNumber = request.AllocateNumber,
                    MaterialId = request.MaterialId,
                    MaterialName = request.MaterialName,
                    SpecificationModel = request.SpecificationModel,
                    Unit = request.Unit,
                    AllocateCount = request.AllocateCount,
                    InventoryCount = request.InventoryCount,
                    Summary = request.Summary
                };
                var res1 = await InventoryDetailRepository.Add(res);
                if (res1 != 0)
                {
                    result.Code = 1;
                    result.Msg = "库存调拨详情新增成功";
                }



            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
